package cn.wolfcode.cms.service.impl;

import cn.wolfcode.cms.domain.Member;
import cn.wolfcode.cms.domain.MemberSpend;
import cn.wolfcode.cms.domain.Membernum;
import cn.wolfcode.cms.mapper.MemberMapper;
import cn.wolfcode.cms.mapper.MemberSpendMapper;
import cn.wolfcode.cms.mapper.MembernumMapper;
import cn.wolfcode.cms.query.MemberQueryObject;
import cn.wolfcode.cms.service.IMemberService;
import cn.wolfcode.cms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MemberServiceImpl implements IMemberService {

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private MemberSpendMapper memberSpendMapper;
    @Autowired
    private MembernumMapper membernumMapper;

    public void save(Member entity) {
        memberMapper.insert(entity);
    }

    public void update(Member entity) {
        memberMapper.updateByPrimaryKey(entity);
    }

    public void delete(Long id) {
        memberMapper.deleteByPrimaryKey(id);
    }

    public Member get(Long id) {
        return memberMapper.selectByPrimaryKey(id);
    }

    public List<Member> list() {
        return memberMapper.selectAll();
    }


    public Object query(MemberQueryObject qo) {
        //查总数
        int total = memberMapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        //查数据
        List<Member> rows = memberMapper.queryForList(qo);
        return new PageResult(total, rows);
    }

    public List<Member> getMemberByExchangeId(Long id) {
        return memberMapper.getMemberByExchangeId(id);
    }

    public void moveMember(Long memberId, Long groupId) {
        memberMapper.moveMember(memberId, groupId);

    }

    public List<MemberSpend> getMax() {
        return memberSpendMapper.selectMax();
    }

    public int getTotalNum() {
        return memberMapper.getTotalNum();
    }

    public List<Integer> getBrithMenBer() {
        ArrayList<Integer> nums = new ArrayList<>();
        Calendar cale = Calendar.getInstance();
        int day = cale.get(Calendar.DATE);
        int month = cale.get(Calendar.MONTH) + 1;
        int brithDay = memberMapper.getBrithMenBer(month, day);
        int brithMonth = memberMapper.getBrithMonthMenBer(month);
        nums.add(brithDay);
        nums.add(brithMonth);
        return nums;
    }

    public Map<String, Object> getMemByKeyInfo(String keyInfo) {
        Map<String, Object> info = memberMapper.getMemByKeyInfo(keyInfo);
        Date birthday = (Date) info.get("birthday");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(birthday);
        info.put("birthday", format);
        return info;
    }

    public Membernum getRecordBykeyword(String keyword) {
        Membernum recordBykeyword = membernumMapper.getRecordBykeyword(keyword);
        return recordBykeyword;
    }

    public List<Membernum> getMemberInfoByCardNum(String cardnum) {
        return membernumMapper.getNumByCard(cardnum);
    }

    @Override
    public int getTotalSpend() {
        return membernumMapper.getTotalSpend();
    }
}
